package com.example.poemtest.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.poemtest.R;
import com.example.poemtest.index.activity.StudyPoetryDetailActivity;
import com.example.poemtest.index.entity.Poetry;
import com.example.poemtest.utils.Constant;
import com.example.poemtest.utils.FullScreenUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 我的收藏页面
 */
public class MyFavouriteActivity extends AppCompatActivity {

    private ImageView next;
    private ListView listView;
    private List<Poetry> myPoetryList;
    private MyFavPoetryAdapter myFavPoetryAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FullScreenUtil.getInstance().fullScreen(MyFavouriteActivity.this,true);
        setContentView(R.layout.activity_my_favourite);
        //绑定控件对象
        setView();
        //设置数据源
        setData();
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //初始化适配器
        myFavPoetryAdapter = new MyFavPoetryAdapter(
                this,
                Constant.favPoetry,
                R.layout.layout_poetry_dynasty_item
        );
        //绑定适配器
        listView.setAdapter(myFavPoetryAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Poetry poetry = new Poetry();
//        Log.e("名字",dataList.get(position).getPoetry_name().toString());
                poetry.setPoetry_name(Constant.favPoetry.get(position).getTitle());
                poetry.setPoetry_author(Constant.favPoetry.get(position).getAutuor());
                poetry.setPoetry_author_dynasty(Constant.favPoetry.get(position).getDynasty());
                poetry.setPoetry_content(Constant.favPoetry.get(position).getContent());
                poetry.setPoetry_desc(Constant.favPoetry.get(position).getTranslation());
//        Log.e("1名字",poetry.getPoetry_name());
                Intent intent = new Intent(MyFavouriteActivity.this, StudyPoetryDetailActivity.class);
                intent.putExtra("poetry", Constant.favPoetry.get(position));
//        Log.e("诗词",poetry.toString());
                startActivity(intent);
            }
        });
    }

    /**
     * 设置数据源
     */
    private void setData() {
        myPoetryList = new ArrayList<>();
        Poetry poetry = new Poetry();
        poetry.setPoetry_name("我收藏的诗");
        poetry.setPoetry_author("收藏");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
//获取当前时间
        Date date = new Date(System.currentTimeMillis());
        poetry.setCreatDate(date);
        myPoetryList.add(poetry);
        Poetry poetry1 = new Poetry();
        poetry1.setPoetry_name("我收藏的诗");
        poetry1.setPoetry_author("收藏");
        poetry.setCreatDate(date);
        myPoetryList.add(poetry1);
    }

    /**
     * 绑定控件对象
     */
    private void setView() {
        next = findViewById(R.id.setup_safe_to_mine);
        listView = findViewById(R.id.my_poetry_list_view);
    }

    @Override
    protected void onResume() {
        super.onResume();
        myFavPoetryAdapter = new MyFavPoetryAdapter(
                this,
                Constant.favPoetry,
                R.layout.layout_poetry_dynasty_item
        );
        //绑定适配器
        listView.setAdapter(myFavPoetryAdapter);
    }
}